// miniprogram/pages/jiaruhuodong/jiaruhuodong.js
const DB = wx.cloud.database();
const _ = DB.command
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    orderItem: '',
    collection: [],
    cunzai: 'cunzai'
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  jiaru() {
    let that = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否要加入该活动？',
      confirmText: "是",
      cancelText: "否",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中'
          })
          wx.cloud.callFunction({
            name: "update",
            data: {
              action: "pushxmid",
              id: that.data.id,
              bmuser: {
                phonenumber: that.data.phonenumber,
                name: that.data.bmname
              }
            }
          }).then(res => {
            console.log(res);
            wx.hideLoading({
              success: (res) => {},
            })
            if (res.result.data.stats.updated === 1) {
              wx.showToast({
                title: '报名成功',
              })
              that.setData({
                show: false,
                cunzai: 'cunzai'
              })
            }
          })
        } else if (res.cancel) {}
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.callFunction({
      name: 'login'
    }).then((res) => {
      DB.collection('huodong').where({
        _id: options.id,
        bm_openid: _.in([res.result.OPENID])
      }).get().then((res) => {
        console.log(res)
        if (res.data.length > 0) {
          this.setData({
            cunzai: 'cunzai'
          })
        } else {
          console.log('aaa')
          this.setData({
            cunzai: 'nocunzai'
          })
        }
      })
      console.log(res)
      this.setData({
        id: options.id,
        _openid: res.result.OPENID
      })
    })
    console.log(options)
    DB.collection('huodong').doc(options.id)
      .get()
      .then((res) => {
        console.log(res)
        this.setData({
          orderItem: res.data,
          id: options.id
        })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onChangea(event) {
    this.setData({
      bmname: event.detail
    })
  },
  onChangeb(event) {
    this.setData({
      phonenumber: event.detail
    })
  },
})